package com.choosefine.it.basedata.controller;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.basedata.model.City;
import com.choosefine.it.basedata.model.Province;
import com.choosefine.it.basedata.model.ProvinceAndCity;
import com.choosefine.it.basedata.model.Region;
import com.choosefine.it.basedata.service.CityService;
import com.choosefine.it.basedata.service.ProvinceService;
import com.choosefine.it.basedata.service.RegionService;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 省市区相关控制层
 *
 * @author guwenwei
 * @date 2016/11/20
 */
@RestController
@RequestMapping("/basedata/area")
@Api(value = "省市区相关", description = "顾文伟")
// 2016/11/24修改将省市区归为地区控制层
public class AreaController extends BaseController {

    @Autowired
    private ProvinceService provinceService;

    @Autowired
    private CityService cityService;

    @Autowired
    private RegionService regionService;

    private final String[] arrStr = {"110000", "120000", "310000", "500000"};//直辖市ID

    @ApiOperation(value = "获取省列表", notes = "获取所有省直辖市列表", response = Province.class)
    @RequestMapping(value = "/provincelist", method = RequestMethod.GET)
    public String getAllProvince() {
        String result = null;
        List<Province> provinceList = provinceService.selectAll();
        if (provinceList != null) {
            result = responseArraySuccess(provinceList);
        } else {
            result = responseFail(ResultCode.MSG_ERROR);
        }
        return result;
    }


    @ApiOperation(value = "获取省内相应城市列表", notes = "传入省的ID号，根据省的ID获取城市列表,若是直辖市则显示区县", response = City.class)
    @ApiImplicitParam(name = "father", value = "父类ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/citylist/{father}", method = RequestMethod.GET)
    public String getCitysByFather(@PathVariable String father) {
        String result = null;
        if (father != null) {

            List<City> cityList = cityService.getCitysByFather(father);

            if (cityList != null) {
                result = responseArraySuccess(cityList);
            } else {
                result = responseFail(ResultCode.MSG_ERROR);
            }
        } else {
            result = responseFail(ResultCode.MSG_PARAMS_NULL);
        }
        return result;
    }

    @ApiOperation(value = "根据ID获取省", notes = "传入省ID,根据ID获取相应省或直辖市", response = Province.class)
    @ApiImplicitParam(name = "provinceId", value = "省ID", required = true, dataType = "Integer", paramType = "path")
    @RequestMapping(value = "/province/{provinceId}", method = RequestMethod.GET)
    public String getProvinceById(@PathVariable String provinceId) {
        String result = null;
        Province province = provinceService.selectByProvinceID(provinceId);
        if (provinceId != null && !"".equals(provinceId)) {
            if (province != null) {
                result = responseSuccess(province);
            } else {
                result = responseFail(ResultCode.MSG_ERROR);
            }
        } else {
            result = responseFail(ResultCode.MSG_PARAMS_NULL);
        }
        return result;
    }

    @ApiOperation(value = "获取城市", notes = "根据id获取相应城市")
    @ApiImplicitParam(name = "cityid", value = "城市ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/city/{cityid}", method = RequestMethod.GET)
    public String getCityById(@PathVariable String cityid) {
        String result = null;
        City city = cityService.selectByCtiyID(cityid);
        if (cityid != null) {
            if (city != null) {
                result = responseSuccess(city);
            } else {
                result = responseFail(ResultCode.MSG_ERROR);
            }
        } else {
            result = responseFail(ResultCode.MSG_PARAMS_NULL);
        }
        return result;
    }

    @ApiOperation(value = "根据ID获取地区", notes = "传入地区ID号，根据ID获取相应地区")
    @ApiImplicitParam(name = "regionId", value = "地区ID", required = true, dataType = "Integer", paramType = "path")
    @RequestMapping(value = "/region/{regionId}", method = RequestMethod.GET)
    public String getRegionById(@PathVariable String regionId) {
        String result = null;
        Region region = regionService.selectByRegionID(regionId);
        if (regionId != null && !"".equals(regionId)) {
            if (region != null) {
                result = responseSuccess(region);
            } else {
                result = responseFail(ResultCode.MSG_ERROR);
            }
        } else {
            result = responseFail(ResultCode.MSG_PARAMS_NULL);
        }
        return result;
    }

    @ApiOperation(value = "获取城市列表", notes = "获取所有城市列表")
    @RequestMapping(value = "/citylist", method = RequestMethod.GET)
    public String getAllCity() {
        String result = null;
        List<City> cityList = cityService.selectAll();

        if (cityList != null) {
            result = responseArraySuccess(cityList);
        } else {
            result = responseFail(ResultCode.MSG_ERROR);
        }
        return result;
    }

    @ApiOperation(value = "获取市内相应地区列表", notes = "根据市ID信息获取地区列表")
    @ApiImplicitParam(name = "father", value = "市ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/regionlist/{father}", method = RequestMethod.GET)
    public String getRegionByFather(@PathVariable String father) {
        String result = null;
        List<Region> regionList = regionService.getRegionsByFather(father);
        if (father != null) {
            if (regionList != null) {
                result = responseArraySuccess(regionList);
            } else {
                result = responseFail(ResultCode.MSG_ERROR);
            }
        } else {
            result = responseFail(ResultCode.MSG_PARAMS_NULL);
        }
        return result;
    }

    @ApiOperation(value = "根据Code获取地区", notes = "传入地区Code，根据Code获取相应地区")
    @ApiImplicitParam(name = "areaCode", value = "地区Code", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/areaCode/{areaCode}", method = RequestMethod.GET)
    public String getAreaByCode(@PathVariable String areaCode) {
        String result = null;
        if (areaCode != null && !"".equals(areaCode)) {
            if (StringUtils.getStrHaveCheckStrNum(areaCode, "0") == 0) {
                result = responseSuccess(regionService.selectByRegionID(areaCode));
            } else if (StringUtils.getStrHaveCheckStrNum(areaCode, "0") == 2) {
                result = responseSuccess(cityService.selectByCtiyID(areaCode));
            } else if (StringUtils.getStrHaveCheckStrNum(areaCode, "0") == 4) {
                result = responseSuccess(provinceService.selectByProvinceID(areaCode));
            }
        } else {
            result = responseFail(ResultCode.MSG_PARAMS_NULL);
        }
        return result;
    }

    @ApiOperation(value = "查找所有省和市", response = ProvinceAndCity.class)
    @RequestMapping(value = "/getAllProvinceAndCity", method = RequestMethod.GET)
    public String getAllProvinceAndCity() {
        String result = null;
        List<ProvinceAndCity> list = cityService.getAllProvinceAndCity();
        if (list != null) {
            result = responseArraySuccess(list);
        } else {
            result = responseFail(ResultCode.MSG_ERROR);
        }
        return result;
    }
}
